home *** CD-ROM | disk | FTP | other *** search
- Path: odi.com!usenet
- From: Kimberley Burchett <burchett>
- Newsgroups: comp.lang.c++
- Subject: Re: Visibility Restricted to Compiler
- Date: 2 Feb 1996 20:49:20 GMT
- Organization: Software Leverage, Inc
- Message-ID: <4ettcg$d1f@mastermind.odi.com>
- References: <4eq8td$hpu@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: loon.odi.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1N (X11; I; SunOS 4.1.3 sun4c)
- X-URL: news:4eq8td$hpu@vixen.cso.uiuc.edu
-
- sjmccaug@prairienet.org (Scott J. McCaughrin) wrote:
- >
- > Is a constructor still visibile to a C++ compiler if it is private? I
- > want a constructor invoked only by compiler, and not e.g. by some
- > user-defined 'new'.
-
- If the constructor is private, it can only be called by the class itself.
- It can't even be called implicitly by new. For example, a private constructor
- means you cannot do this:
-
- void foo()
- {
- Bar bar; // can't construct bar because bar::bar is private
- Bar* barptr; // okay -- constructor not called yet
- barptr = new Bar; // not okay -- can't call bar::bar
- }
-
- The only reasons I can think of that you'd want a private constructor are
- 1) your class provides a bunch of static member functions, but isn't meant
- to be instantiated, or 2) your class uses some other member function to
- create the object -- for example a function named "create" that returns a
- pointer or reference to an object. This last example is often used when
- you really want to return a pointer to some derived class.
-
- Kimberley
-
-